const user = {
  username: 'zhangsan',
  age: 18,
  _password: '***' // 密码不能被遍历
}
const userProxy = new Proxy(user, {
  get(target, prop) {
    if (prop.startsWith('_')) {
      throw new Error('不可访问')
    } else {
      // return target[prop]
      return Reflect.get(target, key)
    }
  },
  set(target, prop, val) {
    if (prop.startsWith('_')) {
      throw new Error('不可修改')
    } else {
      // target[prop] = val
      Reflect.set(target, key, val)
      return true
    }
  },
  deleteProperty(target, prop) {
    if (prop.startsWith('_')) {
      throw new Error('不可删除')
    } else {
      Reflect.defineProperty(target, key)
      // delete target[prop]
      return true
    }
  },
  // 密码不能枚举
  ownKeys(target) {
    return Reflect.ownKeys(target).filter(key => !key.startsWith('_'))
    // return Object.keys(target).filter(key => !key.startsWith('_'))
  }
})

try {
  delete userProxy._password
} catch (e) {
  console.error(e.message) // 不可删除
}
const keys = Object.keys(userProxy)
console.log(keys) // ['username', 'age']
